/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.afaker.searth.common;

import com.afaker.searth.core.index.DefaultIndexer;
import com.afaker.searth.core.index.impl.FileIndexer;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.lucene.index.IndexReader;

/**
 *
 * @author bruce
 */
public class DocumentManager {

    public static void deleteAllDocument() {
        try {
            Properties properties = new Properties();
            properties.load(DefaultIndexer.class.getClassLoader().getResourceAsStream("index.properties"));
            String indexDir = properties.getProperty("index.dir");
            IndexReader reader = IndexReader.open(indexDir);
            for (int i = 0; i < reader.maxDoc(); i++) {
                reader.deleteDocument(i);
            }
            reader.close();
        } catch (IOException ex) {
            Logger.getLogger(DocumentManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void updateAllDocument(String directoryPath) {
        DocumentManager.deleteAllDocument();
        try {
            FileIndexer indexer = new FileIndexer();
            indexer.setDirectoryPath(directoryPath);
            indexer.index();
        } catch (IOException ex) {
            Logger.getLogger(DocumentManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
